package com.lw.controller;

import com.lw.common.utils.ResultVo;
import com.lw.entity.table.TbDictDataTableDef;
import com.lw.entity.table.TbDictTypeTableDef;
import com.lw.service.TbDictDataService;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import com.lw.entity.TbDictType;
import com.lw.service.TbDictTypeService;

import java.util.List;

/**
 * 字典类型 控制层。
 *
 * @author lw
 * @since 2025年05月06日 19:01:57
 */
@RestController
@Tag(name = "字典类型接口")
@RequestMapping("/tbDictType")
public class TbDictTypeController {

    @Resource
    private TbDictTypeService tbDictTypeService;

    @Resource
    private TbDictDataService tbDictDataService;

    /**
     * 添加字典类型。
     *
     * @param tbDictType 字典类型
     * @return {@code true} 添加成功，{@code false} 添加失败
     */
    @PostMapping("save")
    @Operation(summary = "保存字典类型")
    public ResultVo<Boolean> save(@RequestBody @Parameter(description = "字典类型") TbDictType tbDictType) {
        return ResultVo.success(tbDictTypeService.saveDictTypeAndDictData(tbDictType));
    }

    /**
     * 根据主键删除字典类型。
     *
     * @param id 主键
     * @return {@code true} 删除成功，{@code false} 删除失败
     */
    @DeleteMapping("remove/{id}")
    @Operation(summary = "根据主键字典类型")
    public ResultVo<Boolean> remove(@PathVariable @Parameter(description = "字典类型主键") Long id) {
        return ResultVo.success(tbDictTypeService.removeById(id));
    }

    /**
     * 根据主键更新字典类型。
     *
     * @param tbDictType 字典类型
     * @return {@code true} 更新成功，{@code false} 更新失败
     */
    @PutMapping("update")
    @Operation(summary = "根据主键更新字典类型")
    public ResultVo<Boolean> update(@RequestBody @Parameter(description = "字典类型主键") TbDictType tbDictType) {
        return ResultVo.success(tbDictTypeService.updateDictTypeAndDictData(tbDictType));
    }

    /**
     * 查询所有字典类型。
     *
     * @return 所有数据
     */
    @GetMapping("list")
    @Operation(summary = "查询所有字典类型")
    public ResultVo<List<TbDictType>> list() {
        return ResultVo.success(tbDictTypeService.list());
    }

    /**
     * 根据字典类型主键获取详细信息。
     *
     * @param id 字典类型主键
     * @return 字典类型详情
     */
    @GetMapping("getInfo/{id}")
    @Operation(summary = "根据主键获取字典类型")
    public ResultVo<TbDictType> getInfo(@PathVariable @Parameter(description = "字典类型主键") Integer id) {
        TbDictType tbDictType = tbDictTypeService.getById(id);
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq(TbDictDataTableDef.TB_DICT_DATA.DICT_TYPE_ID.getName(), id);
        tbDictType.setDataList(tbDictDataService.list(queryWrapper));
        return ResultVo.success(tbDictType);
    }

    /**
     * 分页查询字典类型。
     *
     * @return 分页对象
     */
    @GetMapping("page")
    @Operation(summary = "分页查询字典类型")
    public ResultVo<Page<TbDictType>> page(@Parameter(description = "分页信息")  TbDictType tbDictType,
                                 @RequestParam(defaultValue = "1") int pageNumber,
                                 @RequestParam(defaultValue = "10") int pageSize) {
        Page page = new Page(pageNumber, pageSize);
        QueryWrapper queryWrapper = new QueryWrapper();
        //名称
        if (StringUtils.hasLength(tbDictType.getDictName())) {
            queryWrapper.like(TbDictTypeTableDef.TB_DICT_TYPE.DICT_NAME.getName(), tbDictType.getDictName());
        }
        //类型
        if (StringUtils.hasLength(tbDictType.getDictType())) {
            queryWrapper.eq(TbDictTypeTableDef.TB_DICT_TYPE.DICT_TYPE.getName(), tbDictType.getDictType());
        }
        queryWrapper.orderBy(TbDictTypeTableDef.TB_DICT_TYPE.ID.getName(), false);
        Page result = tbDictTypeService.page(page, queryWrapper);
        List<TbDictType> records = result.getRecords();
        for (TbDictType record : records) {
            QueryWrapper queryWrapper1 = new QueryWrapper();
            queryWrapper1.eq(TbDictDataTableDef.TB_DICT_DATA.DICT_TYPE_ID.getName(), record.getId());
            record.setDataList(tbDictDataService.list(queryWrapper1));
        }
        return ResultVo.success(result);
    }

    @GetMapping("all")
    @Operation(summary = "所有字典数据")
    public ResultVo<List<TbDictType>> all() {
        List<TbDictType> list = tbDictTypeService.getAllList();
        return ResultVo.success(list);
    }
}
